#include <stdio.h>

#include "Engine.h"

double V(double V0, double V1, double H, double x) {
    if (H == 0) {
        return -1;
    }

    return V0 - x * (V0 - V1) / H;
}

int FrontStep (struct Wave * wave) {
    if (wave == 0) {
        return -1;
    }

    if (wave->lines == 0) {
        return -1;
    }

    if (wave->linesNumber < 1) {
        return -1;
    }

    double V0 = wave->V0;
    double V1 = wave->V1;
    double H = wave->height;
    for (int i = 0; i < wave->linesNumber; i++) {
        (wave->lines + i)->x_prev = (wave->lines + i)->x_current;
        (wave->lines + i)->y_prev = (wave->lines + i)->y_current;
        (wave->lines + i)->x_current += (wave->lines + i)->vx * wave->timeStep;
        (wave->lines + i)->y_current += (wave->lines + i)->vy * wave->timeStep;

        (wave->lines + i)->vx = V(V0, V1, H, (wave->lines + i)->x_current);

        if ((wave->lines + i)->vx <= 0) {
            (wave->lines + i)->vx = 0;
            return 1;
        }

        if ((wave->lines + i)->x_current >= wave->height) {
            return 1;
        }
    }

    return 0;
}
